package com.ynr.sheet.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ynr.common.model.SheetBalance;
import com.ynr.common.model.SheetCash;
import com.ynr.common.model.SheetIncome;
import com.ynr.common.utils.ExcelGenerator;
import com.ynr.dao.ICompanyDao;
import com.ynr.dao.ISheetDao;
import com.ynr.sheet.service.ISheetService;

@Controller
public class SheetController {

	private static final Logger logger = LoggerFactory.getLogger(SheetController.class);
	
	@Autowired
	private ISheetService sheetService;
	
	@Autowired
	private ISheetDao sheetDao;
	
	@Autowired
	private ICompanyDao companyDao;
	
	@RequestMapping("/isSheetInfoExist")
	@ResponseBody
	public Map<String, Object> isSheetInfoExist(HttpServletRequest request) {
		Map<String, Object> retMap = new HashMap<>();
		String companyId = request.getParameter("company_id");
		String billDate = request.getParameter("bill_date");
		String sheetType = request.getParameter("sheet_type");
		if(sheetType.equals("资产负债表")) {
			SheetBalance sb = sheetDao.getSheetBalance(companyId, billDate);
			if(sb != null) {
				retMap.put("isSheetInfoExist", 1);
				return retMap;
			} else {
				retMap.put("isSheetInfoExist", 0);
				return retMap;
			}
		} else if(sheetType.equals("现金流量表")) {
			SheetCash sc = sheetDao.getSheetCash(companyId, billDate);
			if(sc != null) {
				retMap.put("isSheetInfoExist", 1);
				return retMap;
			} else {
				retMap.put("isSheetInfoExist", 0);
				return retMap;
			}
		} else if(sheetType.equals("利润表")) {
			SheetIncome si = sheetDao.getSheetIncome(companyId, billDate);
			if(si != null) {
				retMap.put("isSheetInfoExist", 1);
				return retMap;
			} else {
				retMap.put("isSheetInfoExist", 0);
				return retMap;
			}
		}
		return null;
	}
	
	@RequestMapping("/downloadSheet")
	@ResponseBody
	public ResponseEntity<byte[]> generateSheet(HttpServletRequest request) {
		String companyId = request.getParameter("companyId");
		String billDate = request.getParameter("billDate");
		String sheetType = request.getParameter("sheetType");
		Map<String, Object> companyMap = companyDao.getCompanyById(Long.valueOf(companyId));
		if(sheetType.equals("资产负债表")) {
			SheetBalance sb = sheetDao.getSheetBalance(companyId, billDate);
			return ExcelGenerator.generateSheetBalanceExcel(sb, (String)companyMap.get("name"), billDate, sheetType);
		} else if(sheetType.equals("现金流量表")) {
			SheetCash sc = sheetDao.getSheetCash(companyId, billDate);
			return ExcelGenerator.generateSheetCashExcel(sc, (String)companyMap.get("name"), billDate, sheetType);
		} else if(sheetType.equals("利润表")) {
			SheetIncome si = sheetDao.getSheetIncome(companyId, billDate);
			return ExcelGenerator.generateSheetIncomeExcel(si, (String)companyMap.get("name"), billDate, sheetType);
		}
		return null;
	}
	
	@RequestMapping("/getSheet")
	@ResponseBody
	public Map<String, Object> getSheet(HttpServletRequest request) {
		Map<String, Object> retMap = new HashMap<>();
		String companyId = request.getParameter("company_id");
		String billDate = request.getParameter("bill_date");
		String sheetType = request.getParameter("sheet_type");
		logger.debug("SheetController getSheet params, companyId:{}, billDate:{}, sheetType:{}", companyId, billDate, sheetType);
		retMap = sheetService.getSheet(companyId, billDate, sheetType);
		return retMap;
	}
	
}
